package ui;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JLabel;
import servicios.GenericBusinessException;
import util.GuiUtilities;
import javax.swing.JComboBox;

import dominio.Criterio;

public class UIMenu implements Observer {
	private JDialog jDialog = null;  //  @jve:decl-index=0:visual-constraint="10,10"
	private JPanel jContentPaneMenuPrincipal = null;
	private JButton btnProducto = null;
	private JButton btnPedido = null;
	private JButton btnCliente = null;
	private JLabel ABM = null;
	private JButton btnCriterio = null;
	private ModeloPlanProduccion modeloPlanProduccion=new ModeloPlanProduccion();  //  @jve:decl-index=0:
	private JButton btnListados = null;
	private JComboBox jComboBox = null;

	public UIMenu(){
		super();
		modeloPlanProduccion.agregarObservador(this);
	}
	
	/**
	 * This method initializes jDialog	
	 * 	
	 * @return java.swing.JDialog	
	 */
	private JDialog getJDialog() {
		if (jDialog == null) {
			jDialog = new JDialog();
			jDialog.setSize(new Dimension(464, 318));
			jDialog.setLocationRelativeTo(null);
			jDialog.setTitle("Fabrica");
			jDialog.setContentPane(getJContentPaneMenuPrincipal());
		}
		return jDialog;
	}

	/**
	 * This method initializes jContentPaneMenuPrincipal	
	 * 	
	 * @return java.swing.JPanel	
	 */
	private JPanel getJContentPaneMenuPrincipal() {
		if (jContentPaneMenuPrincipal == null) {
			ABM = new JLabel();
			ABM.setBounds(new Rectangle(26, 38, 106, 21));
			ABM.setText("Mantenimientos");
			jContentPaneMenuPrincipal = new JPanel();
			jContentPaneMenuPrincipal.setLayout(null);
			jContentPaneMenuPrincipal.add(getBtnProductos(), null);
			jContentPaneMenuPrincipal.add(getBtnPedidos(), null);
			jContentPaneMenuPrincipal.add(getBtnClientes(), null);
			jContentPaneMenuPrincipal.add(ABM, null);
			jContentPaneMenuPrincipal.add(getBtnCriterio(), null);
			jContentPaneMenuPrincipal.add(getBtnListados(), null);
			jContentPaneMenuPrincipal.add(getJComboBox(), null);
			getComboItems();
			checkPlanProduccion();
		}
		return jContentPaneMenuPrincipal;
	}





	private void getComboItems() {
		try {
			List<Object> listaCriterios=Fachada.getCriterios();
			for(int i=0;i<listaCriterios.size();i++){
				getJComboBox().addItem(listaCriterios.get(i));
			}
		} catch (GenericBusinessException e) {
			GuiUtilities.showError(this.jContentPaneMenuPrincipal,e.getMessage());			
		}
		
	}

	/**
	 * This method initializes btnTelefonista	
	 * 	
	 * @return java.swing.JButton	
	 */
	private JButton getBtnProductos() {
		if (btnProducto == null) {
			btnProducto = new JButton();
			btnProducto.setBounds(new Rectangle(27, 77, 90, 25));
			btnProducto.setText("Producto");
			btnProducto.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					UIProducto uiProducto=new UIProducto(modeloPlanProduccion);
						uiProducto.setVisible(true);
				}
			});
		}
		return btnProducto;
	}

	/**
	 * This method initializes btnAdministrador	
	 * 	
	 * @return java.swing.JButton	
	 */
	private JButton getBtnPedidos() {
		if (btnPedido == null) {
			btnPedido = new JButton();
			btnPedido.setBounds(new Rectangle(27, 199, 90, 25));
			btnPedido.setText("Pedidos");
			btnPedido.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					UIPedido uiPedido=new UIPedido(modeloPlanProduccion);
					uiPedido.setVisible(true);		
				}
			});
		}
		return btnPedido;
	}

	/**
	 * This method initializes btnTecnico	
	 * 	
	 * @return java.swing.JButton	
	 */
	private JButton getBtnClientes() {
		if (btnCliente == null) {
			btnCliente = new JButton();
			btnCliente.setBounds(new Rectangle(27, 138, 90, 25));
			btnCliente.setText("Clientes");
			btnCliente.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					UICliente uiCliente=new UICliente();
					uiCliente.setVisible(true);
				}
			});
		}
		return btnCliente;
	}
	/**
	 * This method initializes btnCriterio	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCriterio() {
		if (btnCriterio == null) {
			btnCriterio = new JButton();
			btnCriterio.setBounds(new Rectangle(245, 239, 200, 31));
			btnCriterio.setText("Guardar Criterio Produccion");
			btnCriterio.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					guardarCriterio();
				}
			});
		}
		return btnCriterio;
	}


	protected void guardarCriterio() {
		try {
			Criterio seleccion=(Criterio) getJComboBox().getSelectedItem();
			Fachada.setCriterio(seleccion.getId());
			modeloPlanProduccion.hasChanged();
		} catch (GenericBusinessException e) {
			GuiUtilities.showError(this.jContentPaneMenuPrincipal,e.getMessage());			
		}
	}
	
	private void checkPlanProduccion() {
		try {
			int plan=Fachada.getCriterio();
			for(int i=0;i<getJComboBox().getItemCount();i++){
				Criterio criterio=(Criterio) getJComboBox().getItemAt(i);
				if(criterio.getId()==plan){
					getJComboBox().setSelectedItem(criterio);
				}
			}
			
			
		} catch (GenericBusinessException e) {
			GuiUtilities.showError(this.jContentPaneMenuPrincipal,e.getMessage());	
		}
		
		
	}
	

	/**
	 * This method initializes btnListados	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnListados() {
		if (btnListados == null) {
			btnListados = new JButton();
			btnListados.setBounds(new Rectangle(263, 77, 90, 25));
			btnListados.setText("Listados");
			btnListados.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					UIListados uiListado=new UIListados();
					uiListado.setVisible(true);
				}
			});
		}
		return btnListados;
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.setBounds(new Rectangle(244, 196, 200, 22));
		}
		return jComboBox;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		UIMenu ventanaPrincipal =new UIMenu();
		ventanaPrincipal.initPlanProduccion();
		ventanaPrincipal.getJDialog().setVisible(true);
	}

	private void initPlanProduccion() {
		UIPlanProduccion uiPlanProduccion=new UIPlanProduccion(modeloPlanProduccion);
		uiPlanProduccion.setVisible(true);		

		uiPlanProduccion.setSize(new Dimension(772, 355));
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		
	}

}
